#include "ClientFull.h"

const char* ClientFull::LDAP_Password = "secret";

ClientFull::ClientFull(char *CN,char *OU,char **DN,int DN_level):Client(CN,OU,DN,DN_level)
{

}

ClientFull::ClientFull(char *CN,char **DN,int DN_level):Client(CN,DN,DN_level)
{

}


ClientFull::~ClientFull(void)
{
}

bool ClientFull::password_checking(char *pass)
{
		if(!strcmp(LDAP_Password,pass))
			return true;
		return false;
}

//void ClientFull::DeleteEntry(char *dn,int auth_type,char *passWord,char **optiuni,int nr_opt,char *entry_dn)
//{
//	
//
//	if(Auth_state)
//	{
//		if(this->password_checking(passWord))
//		{
//
//			vector<Atribut<char *>> atr;
//
//			Atribut<char *>a1("dn",entry_dn),a2("changetype","delete");
//			atr.push_back(a1);
//			atr.push_back(a2);
//
//		
//			Delete_Operation *deleteEntry_op = new Delete_Operation(dn,auth_type,passWord,optiuni,nr_opt,atr);
//
//			LDAP_Operation *op = deleteEntry_op->operate();
//
//			this->sendLdifFile(deleteEntry_op->getLdifFile());
//
//	
//			bool iResult = this->sendActionPackets(op);
//		
//			LDAP_Result *delete_res = new LDAP_Result(this->update(),iResult);
//
//			delete_res->afisareResult();
//		}
//		else
//		{
//			printf("\n-> The LDAP Server password is wrong!!...Please try again!");
//			return;
//		}
//	}
//	else
//	{
//		printf("\n->You must be authenticated to LDAP Server to perform this operation!!");
//		return;
//	}
//
//}

void ClientFull::DeleteAtributeFromEntry(char *dn,int auth_type,char *passWord,char **optiuni,int nr_opt,vector<Atribut<char*>> atributes)
{
	if(Auth_state)
	{
		if(this->password_checking(passWord))
		{
			vector<Atribut<char *>> atr;

			Atribut<char *>a1("dn",this->ClientDN),a2("changetype","modify"),a3;
			atr.push_back(a1);
			atr.push_back(a2);
	
			for(int i=0;i<atributes.size();i++)
			{
				a3.setAtribut("delete",atributes[i].daNume());
				atr.push_back(a3);
			}
		
			Modify_Operation *modifyAtribute_op = new Modify_Operation(dn,auth_type,passWord,optiuni,nr_opt,atr);

			LDAP_Operation *op = modifyAtribute_op->operate();

			this->sendLdifFile(modifyAtribute_op->getLdifFile());

	
			bool iResult = this->sendActionPackets(op);
		
			LDAP_Result *bind_res = new LDAP_Result(this->update(),iResult);

			bind_res->afisareResult();
		}
		else
		{
			printf("\n-> The LDAP Server password is wrong!!...Please try again!");
			return;
		}
	}
	else
	{
		printf("\n->You must be authenticated to LDAP Server to perform this operation!!");
		return;
	}
}



void ClientFull::AddAtributes(char *dn,int auth_type,char *passWord,char **optiuni,int nr_opt,vector<Atribut <char *>> atr_add)
{
	if(Auth_state)
	{
		if(this->password_checking(passWord))
		{
			vector<Atribut<char *>> atr;

			Atribut<char *>a1("dn",this->ClientDN),a2("changetype","modify"),a3;
			atr.push_back(a1);
			atr.push_back(a2);
	
			for(int i=0;i<atr_add.size();i++)
			{
				a3.setAtribut("add",atr_add[i].daNume());
				atr.push_back(a3);
				atr.push_back(atr_add[i]);
			}
		
			Modify_Operation *modifyAtribute_op = new Modify_Operation(dn,auth_type,passWord,optiuni,nr_opt,atr);

			LDAP_Operation *op = modifyAtribute_op->operate();

			this->sendLdifFile(modifyAtribute_op->getLdifFile());

	
			bool iResult = this->sendActionPackets(op);
		
			LDAP_Result *bind_res = new LDAP_Result(this->update(),iResult);

			bind_res->afisareResult();
		}
		else
		{
			printf("\n-> The LDAP Server password is wrong!!...Please try again!");
			return;
		}
	}
	else
	{
		printf("\n->You must be authenticated to LDAP Server to perform this operation!!");
		return;
	}
}



void ClientFull::ModifyAtributes(char *dn,int auth_type,char *passWord,char **optiuni,int nr_opt,vector<Atribut<char*>> atribute)
{

	if(Auth_state)
	{
		if(this->password_checking(passWord))
		{
			vector<Atribut<char*>> atr_noi;

			Atribut<char *>a1("dn",this->ClientDN),a2("changetype","modify"),a3;
			atr_noi.push_back(a1);
			atr_noi.push_back(a2);
	
			for(int i=0;i<atribute.size();i++)
			{
				a3.setAtribut("replace",atribute[i].daNume());
				atr_noi.push_back(a3);
				atr_noi.push_back(atribute[i]);
			}
		
			Modify_Operation *modifyAtribute_op = new Modify_Operation(dn,auth_type,passWord,optiuni,nr_opt,atr_noi);

			LDAP_Operation *op = modifyAtribute_op->operate();

			this->sendLdifFile(modifyAtribute_op->getLdifFile());

	
			bool iResult = this->sendActionPackets(op);
		
			LDAP_Result *bind_res = new LDAP_Result(this->update(),iResult);

			bind_res->afisareResult();
		}
		else
		{
			printf("\n-> The LDAP Server password is wrong!!...Please try again!");
			return;
		}
	}
	else
	{
		printf("\n->You must be authenticated to LDAP Server to perform this operation!!");
		return;
	}

}

void ClientFull::AddJPGPhoto_Atribute(char *dn,int auth_type,char *passWord,char **optiuni,int nr_opt,char *JPGFile_name)
{
	/*dn: cn=user1,ou=people,dc=tup,dc=com
changetype: modify
add: jpegPhoto
jpegPhoto: image.jpg*/

	if(Auth_state)
	{
		if(this->password_checking(passWord))
		{

			sendJPGImage(JPGFile_name);

			vector<Atribut<char *>> atr;

			Atribut<char *>a1("dn",this->ClientDN),a2("changetype","modify"),a3("add","jpegPhoto"),a4("jpegPhoto",JPGFile_name);
			atr.push_back(a1);
			atr.push_back(a2);
			atr.push_back(a3);
			atr.push_back(a4);
	
		
			Modify_Operation *modifyAtribute_op = new Modify_Operation(dn,auth_type,passWord,optiuni,nr_opt,atr);

			LDAP_Operation *op = modifyAtribute_op->operate();

			this->sendLdifFile(modifyAtribute_op->getLdifFile());

	
			bool iResult = this->sendActionPackets(op);
		
			LDAP_Result *addJPGAtribute_res = new LDAP_Result(this->update(),iResult);

			addJPGAtribute_res->afisareResult();
		}
		else
		{
			printf("\n-> The LDAP Server password is wrong!!...Please try again!");
			return;
		}
	}
	else
	{
		printf("\n->You must be authenticated to LDAP Server to perform this operation!!");
		return;
	}
}

//void ClientFull::addEntry(char *dn,int auth_type,char *passWord,char **optiuni,int nr_opt,vector<Atribut<char*>> atribute)
//{
//	if(Auth_state)
//	{
//
//		if(this->password_checking(passWord))
//		{
//
//			AddEntry *addentry_op = new AddEntry(dn,auth_type,passWord,optiuni,nr_opt,atribute);
//			
//			LDAP_Operation *op = addentry_op->operate();
//
//			this->sendLdifFile(addentry_op->getLdifFile());
//
//			bool iResult = this->sendActionPackets(op);
//		
//			LDAP_Result *bind_res = new LDAP_Result(this->update(),iResult);
//
//			bind_res->afisareResult();
//
//			
//		}
//		else
//		{
//			printf("\n-> The LDAP Server password is wrong!!...Please try again!");
//			return;
//		}
//	}
//	else
//	{
//		printf("\n->You must be authenticated to LDAP Server to perform this operation!!");
//		return;
//	}
//}


void ClientFull::sendLdifFile(vector<Atribut<char*>> atribute)
{
	if(Auth_state)
	{
		_LDIF ldif_add(atribute);
		char file[100];
		strcpy(file,ldif_add.Da_LDIF());
		ldif_file = new char[strlen(file)+1];
		strcpy(ldif_file,file);

		bool iResult = this->sendActionPackets(ldif_file);
		
		LDAP_Result *bind_res = new LDAP_Result(this->update(),iResult);

		bind_res->afisareResult();

	}
	else
	{
		printf("\n->You must be authenticated to LDAP Server to perform this operation!!");
		return;
	}

}


void ClientFull::sendLdifFile(char *file_name)
{
	if(Auth_state)
	{

		ldif_file = new char[strlen(file_name)+1];
		strcpy(ldif_file,file_name);

		bool iResult = this->sendActionPackets(ldif_file);
		
		LDAP_Result *sendfile_res = new LDAP_Result(this->update(),iResult);

		sendfile_res->afisareResult();

	}
	else
	{
		printf("\n->You must be authenticated to LDAP Server to perform this operation!!");
		return;
	}
}

void ClientFull::sendJPGImage(char *image_name)
{
	if(Auth_state)
	{

		JPG_image = new char[strlen(image_name)+1];
		strcpy(JPG_image,image_name);

		bool iResult = this->sendActionPackets(JPG_image);
		
		LDAP_Result *sendJPG_res = new LDAP_Result(this->update(),iResult);

		sendJPG_res->afisareResult();

	}
	else
	{
		printf("\n->You must be authenticated to LDAP Server to perform this operation!!");
		return;
	}
}